package com.zx.scp.service.util.swift;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * Created by bibei on 2017/4/21.
 */
public class XmlUtils {

	/** <一句话功能简述>
	 * <功能详细描述>request转字符串
	 * @param request
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public static String parseRequst(HttpServletRequest request){
		String body = "";
		try {
			ServletInputStream inputStream = request.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
			while(true){
				String info = br.readLine();
				if(info == null){
					break;
				}
				if(body == null || "".equals(body)){
					body = info;
				}else{
					body += info;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}

	public static String parseStream(InputStream inputStream){
		String body = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
			while(true){
				String info = br.readLine();
				if(info == null){
					break;
				}
				if(body == null || "".equals(body)){
					body = info;
				}else{
					body += info;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
	public static String parseXML(Map<String, String> parameters) {
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry)it.next();
			String k = (String)entry.getKey();
			String v = (String)entry.getValue();
			if (null != v && !"".equals(v) && !"appkey".equals(k)) {
				sb.append("<" + k + ">" + parameters.get(k) + "</" + k + ">\n");
			}
		}
		sb.append("</xml>");
		return sb.toString();
	}


	/**
	 * 转XMLmap
	 * @author
	 * @param inputStream
	 * @param charset
	 * @return
	 * @throws Exception
	 */
	public static Map<String, String> toMap(InputStream inputStream, String charset) throws Exception{
		SAXReader reader = new SAXReader(false);
		InputSource source = new InputSource(inputStream);
		source.setEncoding(charset);
		Document doc = reader.read(source);
		Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
		return params;
	}


	public static Map<String, String> toMap(byte[] xmlBytes,String charset) throws Exception{
		SAXReader reader = new SAXReader(false);
		InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
		source.setEncoding(charset);
		Document doc = reader.read(source);
		Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
		return params;
	}

	/**
	 * 转MAP
	 * @author
	 * @param element
	 * @return
	 */
	public static Map<String, String> toMap(Element element){
		Map<String, String> rest = new HashMap<String, String>();
		List<Element> els = element.elements();
		for(Element el : els){
			rest.put(el.getName().toLowerCase(), el.getTextTrim());
		}
		return rest;
	}

	public static String toXml(Map<String, String> params){
		StringBuilder buf = new StringBuilder();
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		buf.append("<xml>");
		for(String key : keys){
			buf.append("<").append(key).append(">");
			buf.append("<![CDATA[").append(params.get(key)).append("]]>");
			buf.append("</").append(key).append(">\n");
		}
		buf.append("</xml>");
		return buf.toString();
	}
}
